package cz.boxdesign.library.entity.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.boxdesign.library.common.dao.CommonDao;
import cz.boxdesign.library.common.dao.DatabaseOperationException;
import cz.boxdesign.library.entity.Book;
import cz.boxdesign.library.entity.Reservation;
import cz.boxdesign.library.entity.User;
import cz.boxdesign.library.entity.dao.query.OddeleniSqlQuery;
import cz.boxdesign.library.entity.dao.query.RezervaceSqlQuery;

@Component("rezervaceDao")
public class RezervaceDaoImpl extends EntityDaoAbstract<Reservation, RezervaceSqlQuery> {

  @Autowired
  CommonDao commonDao;

  @Override
  public Reservation getById(Integer id) {
    throw new UnsupportedOperationException("This Operationis not Supported !");
  }

  /* (non-Javadoc)
   * @see cz.boxdesign.library.entity.dao.EntityDaoAbstract#updateEntity(cz.boxdesign.library.entity.LibraryEntity)
   */
  @Override
  public void updateEntity(Reservation entity) throws DatabaseOperationException {
    throw new UnsupportedOperationException("This Operationis not Supported !");
  }

  /* (non-Javadoc)
   * @see cz.boxdesign.library.entity.dao.EntityDaoAbstract#batchUpdate(java.util.List)
   */
  @Override
  public void batchUpdate(List<Reservation> entityList) throws DatabaseOperationException {
    throw new UnsupportedOperationException("This Operationis not Supported !");
  }

  @Override
  void getEntityList(List<Reservation> result, ResultSet rs) throws SQLException {
    while (rs.next()) {
      Reservation p = new Reservation();
      try {
        p.setKniha((Book) this.commonDao.getDao(Book.class).getById(rs.getInt("KNIHA_ID_K")));
        p.setUser((User) this.commonDao.getDao(User.class).getById(rs.getInt("UZIVATEL_ID_U")));
      } catch (DatabaseOperationException e) {
        e.printStackTrace();
      }
      result.add(p);
    }
  }

  @Override
  String getSqlQuery(int queryValue) {
    return RezervaceSqlQuery.getByValue(queryValue).getQuery();
  }

  @Override
  void fillStatement(Reservation entity, int queryValue, PreparedStatement ps) throws SQLException {
    OddeleniSqlQuery query = OddeleniSqlQuery.getByValue(queryValue);
    switch (query) {
      case INSERT: {
        ps.setInt(1, entity.getUser().getId());
        ps.setInt(2, entity.getKniha().getId());
        break;
      }
      case DELETE: {
        ps.setInt(1, entity.getUser().getId());
        ps.setInt(2, entity.getKniha().getId());
        break;
      }
      default:
        throw new IllegalArgumentException();
    }
  }


}
